//
//  PicRectionViewCollectionViewCell.m
//  jingdongfang
//
//  Created by RKL on 2018/5/10.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "PicRectionViewCollectionViewCell.h"
@interface PicRectionViewCollectionViewCell ()
@property (nonatomic, strong) UIScrollView *scrollview;
@end
@implementation PicRectionViewCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self privateSetUpUI];
    }
    return self;
}

- (void)privateSetUpUI
{
    self.scrollview.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)setDataSourse:(NSArray *)dataSourse
{
    for (UIView *view in _scrollview.subviews)
    {
        [view removeFromSuperview];
    }
    _dataSourse = dataSourse;
    CGFloat maxX = 0.0;
    CGFloat Margin = 3;
    CGFloat width = ((self.width - 3 * 4)/3);
    for (int i = 0 ; i < dataSourse.count; i ++)
    {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        button.frame = CGRectMake(Margin +(width +Margin)*i, 0, width, width);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000+i;
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 10;
        
        UIImageView * picImageView = [UIImageView new];
        picImageView.frame = CGRectMake(0, 0, width, width);
        picImageView.layer.masksToBounds = YES;
        picImageView.layer.cornerRadius = 10;
        [picImageView sd_setImageWithURL:[NSURL URLWithString:[dataSourse[i] objectForKey:@"image"]]];
        
        
        
        UIImageView * lookImageView = ({
            UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_look"]];
            imageView.frame = CGRectMake(5, width-25, 20, 20);
            imageView;
        });
        [picImageView addSubview:lookImageView];
        
        
        
        UILabel * lookLabel = ({
            UILabel * label = [[UILabel alloc] init];
            label.text = [dataSourse[i] objectForKey:@"browseNum"];
            label.frame = CGRectMake(30, width-25, [self calculateRowWidth:label.text], 20);
            label.textColor = RGB(255, 255, 255);
            label.font = [UIFont systemFontOfSize:12];
            label.textAlignment = NSTextAlignmentCenter;
            label;
        });
        [picImageView addSubview:lookLabel];
        
        
        
        UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(Margin +(width +Margin)*i, CGRectGetMaxY(button.frame) + 10, width, 14)];
        title.text = [dataSourse[i] objectForKey:@"title"];
        title.textAlignment = NSTextAlignmentCenter;
        title.textColor = [UIColor c4Color];
        title.font = [UIFont systemFontOfSize:14];
        
        [self.scrollview addSubview:button];
        [button addSubview:picImageView];
        [self.scrollview addSubview:title];
        maxX = CGRectGetMaxX(button.frame);
    }
    self.scrollview.contentSize = CGSizeMake(maxX + 3, 0);
    
}

#pragma mark - button的点击事件
- (void)buttonClick:(UIButton *)sender
{
    NSLog(@"%ld",sender.tag-1000);
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickList:)])
    {
        [self.delegate clickList:[self.dataSourse[sender.tag-1000] objectForKey:@"id"]];
    }
}



#pragma mark - 计算lable的宽度
- (CGFloat)calculateRowWidth:(NSString *)string
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};  //指定字号
    CGRect rect = [string boundingRectWithSize:CGSizeMake(0, 20)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width;
}

-(UIScrollView *)scrollview
{
    if (_scrollview == nil)
    {
        _scrollview = [UIScrollView new];
        [self addSubview:_scrollview];
    }
    return _scrollview;
}
@end
